package cn.bellychang.ood.mailsendproduct.good;

import cn.bellychang.ood.mailsendproduct.Configuration;

import java.util.List;

/**
 * @author ChangLiang
 * @date 2020/5/12
 */
public class PromotionJob {

    private ProductService productService = new ProductService();

    private UserService userService = new UserService();

    public void run() {

        Configuration cfg = new Configuration();
        MailSender mailSender = new MailSender(cfg);

        // 获取产品列表
        List<Product> products = productService.getPromotionProduct();
        // 根据产品列表 查询用户 填充user中的subscribedProducts
        List<User> users = userService.getUsers(products);
        // 根据用户 开始发送邮件
        for (User user : users) {
            // 这里没有体现出产品的信息 所以可能应该抽象出一个Subscription的概念
            mailSender.sendMail(new Mail(user, new TextMailBodyTemplate()));
        }
    }
}
